package sjsu.android;


import java.sql.Date;
import java.util.HashMap;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;



import dbAdapter.CreateDailyInfoData;
import dbAdapter.DailyAllowance;
import dbAdapter.DailyInfoDBAdapter1;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
public class DailyReportActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);



		setContentView(R.layout.dailyreport);
		CreateDailyInfoData data = new CreateDailyInfoData();
		data.create(this);

		Intent intent = getIntent();
		String dateString= intent.getExtras().getString("Date");
		Date date = Date.valueOf(dateString);

		//Date currDate = null;
		//currDate = new java.sql.Date(System.currentTimeMillis());
		java.util.Date utilDate = new java.util.Date(date.getTime());
		DailyInfoDBAdapter1 db = new DailyInfoDBAdapter1(this);
		db.open();
		int caloriesBurnt = db.getCaloriesBurntForDay(utilDate);
		TextView caloriesBurntTextView = (TextView) findViewById(R.id.caloriesBurntNumTextView);
		caloriesBurntTextView.setText("" + caloriesBurnt);
		Log.e("Anu", "" + caloriesBurnt);
		HashMap<String,Integer> map = db.getCaloriesConsumedForADay(date);
		int caloriesConsumed = db.getCaloriesConsumedForDay(utilDate);

		TextView caloriesConsumedTextView = (TextView) findViewById(R.id.caloriesConsumedNumTextView);
		caloriesConsumedTextView.setText("" + caloriesConsumed);
		Log.e("Anu", "" + caloriesConsumed);
		DailyAllowance da = new DailyAllowance();
		int dailyAllowance = da.getDailyAllowance(this);
		
		//int recDailyAllowance = 2200;
		int netCalories = caloriesConsumed - caloriesBurnt;
		TextView netCaloriesConsumedTextView = (TextView) findViewById(R.id.netCaloriesConsumedNumTextView);
		netCaloriesConsumedTextView.setText("" + netCalories);
		TextView recDailyAllowanceTextView = (TextView) findViewById(R.id.recDailyAllowanceNumTextView);
		recDailyAllowanceTextView.setText(""+dailyAllowance);


		ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
		progressBar.setMax(dailyAllowance);
		progressBar.setProgress(netCalories);

		


		pieChart(map);

		String categoryName = db.getMaxPercentageCategoryInADay(date);
		if (categoryName == null){

		}
		else
		{
			TextView tv = (TextView)findViewById(R.id.highIntake);

			tv.setText("Your "  + categoryName + " intake is high");

		}
	}


		private  void pieChart(HashMap<String, Integer> map) {

			LinearLayout layout = (LinearLayout) findViewById(R.id.piechart);
			CategorySeries series = new CategorySeries("Yearly Report");

			int[] colors = new int[] {Color.BLUE, Color.rgb(200, 100, 50), Color.CYAN,
					Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED,
					Color.WHITE, Color.YELLOW, Color.rgb(50, 150,200), Color.rgb(100, 200, 150), 
					Color.RED
			};
			int i=0;
			DefaultRenderer renderer = new DefaultRenderer();
			for (String category: map.keySet()) {

				series.add(category, map.get(category));
				SimpleSeriesRenderer r = new SimpleSeriesRenderer();
				r.setColor(colors[i]);
				renderer.addSeriesRenderer(r);
				renderer.setLabelsTextSize(12);
				renderer.setLabelsColor(Color.BLACK);
				renderer.setTextTypeface("sans_serif", Typeface.BOLD);
				
				i++;

			}
			renderer.setShowLegend(false);




			GraphicalView mChartView = ChartFactory.getPieChartView(this, series, renderer); 
			layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));



			/*
			WebView webView = (WebView) findViewById(R.id.WebView01);
			//String mUrl = "http://chart.apis.google.com/chart?cht=p3&chd=t:30,60,10&chs=250x100&chl=cars|bikes|trucks";
			String mUrl = "http://chart.apis.google.com/";
			webView.loadUrl(mUrl);
			 */


		}





	}
